//
//  MJPhotoToolbar.m
//  FingerNews
//
//  Created by mj on 13-9-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJPhotoToolbar.h"
#import "MJPhoto.h"

@interface MJPhotoToolbar()
{
    // 显示页码
    UILabel *_indexLabel;
    UIPageControl *_pageControImage ;

    UIButton *_saveImageBtn;
}
@end

@implementation MJPhotoToolbar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setPhotos:(NSArray *)photos
{
    _photos = photos;
    
    if (_photos.count > 1) {
        _pageControImage = [[UIPageControl alloc]init];
         _pageControImage.frame = self.bounds;
        _pageControImage.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _pageControImage.numberOfPages = _photos.count;
        //设置没有显示的点的颜色
        _pageControImage.pageIndicatorTintColor = [ UIColor grayColor];
        _pageControImage.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControImage.enabled = NO;

        
        [self addSubview:_pageControImage];
    }
    
    // 保存图片按钮
    CGFloat btnWidth = self.bounds.size.height;
    _saveImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _saveImageBtn.frame = CGRectMake(20, 0, btnWidth, btnWidth);
    _saveImageBtn.frame = CGRectMake(self.width -btnWidth-20 , 0, btnWidth, btnWidth);

    _saveImageBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    UIImage *img;
    UIImage *selImg;
    SEL sel;
    if (_toolStyle == ToolBtnSave) {
        img = [UIImage imageNamed:@"img_save_n"];
        selImg = [UIImage imageNamed:@"img_save_n"];
        sel = NSSelectorFromString(@"saveImage");
    }else if (_toolStyle == ToolBtnDelete){
        img = [UIImage imageNamed:@"browser_delete_icon"];
        selImg = [UIImage imageNamed:@"browser_delete_icon"];
        sel = NSSelectorFromString(@"deleteImage");
    }else{
        img = [UIImage imageNamed:@"img_save_n"];
        selImg = [UIImage imageNamed:@"img_save_n"];
        sel = NSSelectorFromString(@"saveImage");
    }
    [_saveImageBtn setImage:img forState:UIControlStateNormal];
    [_saveImageBtn setImage:selImg forState:UIControlStateHighlighted];
    [_saveImageBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_saveImageBtn];
}

- (void)saveImage
{
    __weak typeof(self) weakSelf=self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        MJPhoto *photo = weakSelf.photos[weakSelf.currentPhotoIndex];
        UIImageWriteToSavedPhotosAlbum(photo.image, weakSelf,@selector(image:didFinishSavingWithError:contextInfo:), nil);
    });
}

- (void)deleteImage{
    if (_deleteHander) {
        _deleteHander();
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [SAppUtil canPhotoAlbum];
    } else {
        MJPhoto *photo = _photos[_currentPhotoIndex];
        photo.save = true;
        YSAlert *alert = [[YSAlert alloc] init];
        alert.title = KLanguage(@"save_success_txt");
        alert.sureTitle=KLanguage(@"sure_txt_key");
        [alert show];
    }
}

- (void)setCurrentPhotoIndex:(NSUInteger)currentPhotoIndex
{
    _currentPhotoIndex = currentPhotoIndex;
    _pageControImage.currentPage = _currentPhotoIndex;
}

@end
